package com.enterprise.viewer.client.presenter;

import com.enterprise.viewer.client.event.MasonryEvent;
import com.enterprise.viewer.client.event.ServiceEvent;
import com.enterprise.viewer.client.event.ServiceEvent._Event;
import com.enterprise.viewer.client.view.Gadget;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.binder.EventBinder;


public class MasonryPresenter implements Presenter{

	interface MyEventBinder extends EventBinder<MasonryPresenter> {}
	static MyEventBinder eventBinder = GWT.create(MyEventBinder.class);

	public interface Display {
		FlexTable getContentPane();
		Widget asWidget();
	}

	private final SimpleEventBus eventBus;
	private final Display display;

	private Integer curcell = 0, currow = 0;


	public MasonryPresenter(SimpleEventBus eventBus, Display view){
		this.eventBus = eventBus;
		this.display = view;
		eventBinder.bindEventHandlers(this, eventBus);
		bind();
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	private void bind(){		

		eventBus.addHandler(ServiceEvent.TYPE, new ServiceEvent._EventHandler() {

			@Override
			public void onChange(_Event event) {
				if(event.getType().compareTo(ServiceEvent.EVENTTYPE.RESPONSE) == 0 || event.getType().compareTo(ServiceEvent.EVENTTYPE.BROADCAST) == 0){
					if(event.getCategory().compareTo(ServiceEvent.CATEGORY.GET) == 0){
						if(event.getName().compareTo(ServiceEvent.NAME.CONTENTPROVIDERS) == 0){
							if(event.getParams() != null && event.getParams().length == 1){
								//updateNavBar((ContentProviders) event.getParams()[0]);
							}
						}
					}
				}
			}});

		eventBus.addHandler(MasonryEvent.TYPE, new MasonryEvent._EventHandler() {

			@Override
			public void onChange(MasonryEvent._Event event) {
				if(event.getType().compareTo(MasonryEvent.EVENTTYPE.RESPONSE) == 0 || event.getType().compareTo(MasonryEvent.EVENTTYPE.BROADCAST) == 0){
					if(event.getCategory().compareTo(MasonryEvent.CATEGORY.CHANGED) == 0){
						if(event.getName().compareTo(MasonryEvent.NAME.REFRESH) == 0){
							if(event.getParams() != null && event.getParams().length == 1){
								setContentProvider((String) event.getParams()[0]);
							}
						}
					}
				}
			}
		});

		History.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				//String token = event.getValue();
			}
		});
	}

	private void setContentProvider(String url){
		Gadget gadget = (Gadget) display.getContentPane().getWidget(currow, curcell);
		
		if(++curcell >= display.getContentPane().getCellCount(0)){
			curcell = 0;

			if(++currow >= display.getContentPane().getRowCount()){
				currow = 0;
			}
		}
		
		
		gadget.setURL("http://" + url);
	}

	private class LoadProviderClickHandler implements ClickHandler {
		private String url;		

		public LoadProviderClickHandler(String url) {
			this.url = url;
		}

		@Override
		public void onClick(ClickEvent event) {

			//http://examples.javacodegeeks.com/enterprise-java/gwt/dynamic-flextable-example/

			FlexTable table = display.getContentPane();
			table.clear();	
			table.insertRow(0);
			table.addCell(0);
			table.addCell(0);
			table.insertRow(0);
			table.addCell(0);
			table.addCell(0);

			//http://stackoverflow.com/questions/9434836/access-frames-document-using-gwt
			//http://www.gwtproject.org/javadoc/latest/com/google/gwt/dom/client/IFrameElement.html

			//IFrameElement iframe = IFrameElement.as(frame.getElement());
			//iframe.getContentDocument();

			String s = "<iframe src='http://" + url + "' height='100%' width='100%' seamless='true' name='myframe' scrolling='no' frameborder='0'></iframe>";

			table.addCell(0);
		}
	}
}
